package com.zzyl.nursing.service.impl;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import com.google.common.collect.Maps;

import java.util.Arrays;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.domain.*;
import com.zzyl.nursing.dto.*;
import com.zzyl.nursing.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.service.ICheckInService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 入住Service业务层处理
 * 
 * @author alexis
 * @date 2025-02-25
 */
@Service
public class CheckInServiceImpl extends ServiceImpl<CheckInMapper, CheckIn> implements ICheckInService
{
    @Autowired
    private CheckInMapper checkInMapper;
    @Autowired
    private ElderMapper elderMapper;
    @Autowired
    private BedMapper bedMapper;
    private ContractMapper contractMapper;
    @Autowired
    private CheckInConfigMapper checkInConfigMapper;

    /**
     * 查询入住
     * 
     * @param id 入住主键
     * @return 入住
     */
    @Override
    public CheckIn selectCheckInById(Long id)
    {
        return checkInMapper.selectById(id);
    }

    /**
     * 查询入住列表
     * 
     * @param checkIn 入住
     * @return 入住
     */
    @Override
    public List<CheckIn> selectCheckInList(CheckIn checkIn)
    {
        return checkInMapper.selectCheckInList(checkIn);
    }

    /**
     * 新增入住
     * 
     * @param checkIn 入住
     * @return 结果
     */
    @Override
    public int insertCheckIn(CheckIn checkIn)
    {
        return checkInMapper.insert(checkIn);
    }

    /**
     * 修改入住
     * 
     * @param checkIn 入住
     * @return 结果
     */
    @Override
    public int updateCheckIn(CheckIn checkIn)
    {
        return checkInMapper.updateById(checkIn);
    }

    /**
     * 批量删除入住
     * 
     * @param ids 需要删除的入住主键
     * @return 结果
     */
    @Override
    public int deleteCheckInByIds(Long[] ids)
    {
        return checkInMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除入住信息
     * 
     * @param id 入住主键
     * @return 结果
     */
    @Override
    public int deleteCheckInById(Long id)
    {
        return checkInMapper.deleteById(id);
    }

    @Override
    public void apply(CheckInApplyDto checkInApplyDto) {
        CheckInElderDto elderDto = checkInApplyDto.getCheckInElderDto();
        List<ElderFamilyDto> elderFamilyDtoList = checkInApplyDto.getElderFamilyDtoList();
        CheckInConfigDto configDto = checkInApplyDto.getCheckInConfigDto();
        CheckInContractDto contractDto = checkInApplyDto.getCheckInContractDto();
        //1.判断老人是否存在
        Elder elder = elderMapper.selectOne(Wrappers.<Elder>lambdaQuery().eq(Elder::getIdCardNo, elderDto.getIdCardNo()));
        if (ObjectUtil.isNotNull(elder) && elder.getStatus().equals(1)){
            throw new RuntimeException("Exist!");
        }
        //2.修改床位状态
        Bed bed = bedMapper.selectById(configDto.getBedId());
        bed.setBedStatus(1);
        bedMapper.updateBed(bed);
        //3.新增或更新老人
        elder.setStatus(1);
        elder.setBedNumber(bed.getBedNumber());
        elder.setBedId(bed.getId());
        if (ObjectUtil.isNull(elder.getId())){
            elder= BeanUtil.toBean(elderDto, Elder.class);
            elderMapper.insert(elder);
        }else {
            elderMapper.updateById(elder);
        }
        //4.新增签约办理
        Contract contract = BeanUtil.toBean(contractDto, Contract.class);
        contract.setElderId(elder.getId().intValue());
        contract.setContractNumber(DateUtils.getDateStr(new Date(), "yyyyMMddHHmmss"));
        contract.setElderName(elder.getName());
        contract.setStatus(1);
        contract.setSortOrder(DateUtils.differentDaysByMillisecond(new Date(), DateUtils.parseDate(elderDto.getBirthday())));
        contractMapper.insert(contract);
        //5.新增入住信息
        CheckIn checkIn = BeanUtil.toBean(configDto, CheckIn.class);
        checkIn.setElderName(elder.getName());
        checkIn.setElderId(elder.getId());
        checkIn.setBedNumber(bed.getBedNumber());
        checkIn.setStatus(0);
        checkIn.setSortOrder(DateUtils.differentDaysByMillisecond(new Date(), DateUtils.parseDate(elderDto.getBirthday())));
       if (CollUtil.isNotEmpty(elderFamilyDtoList)){
            checkIn.setRemark(JSONUtil.toJsonStr(elderFamilyDtoList));
       }
        checkInMapper.insert(checkIn);
        //6.新增入住配置
        CheckInConfig checkInConfig = BeanUtil.toBean(configDto,CheckInConfig.class);
        checkInConfig.setCheckInId(checkIn.getId());//
        checkInConfig.setSortOrder(DateUtils.differentDaysByMillisecond(new Date(), DateUtils.parseDate(elderDto.getBirthday())));
        checkInConfigMapper.insert(checkInConfig);
    }
}
